package com.qf.lanzhou.blog.api.dao.Impl.login;

import com.qf.lanzhou.blog.api.dao.RoleDao;
import com.qf.lanzhou.blog.api.pojo.entity.Role;
import com.qf.lanzhou.blog.api.util.BeanProcessor;
import com.qf.lanzhou.blog.api.util.DruidUtil;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import java.util.ArrayList;
import java.util.List;

/**
 * @author LEON-HE
 */
public class RoleDaoImpl implements RoleDao {
    private QueryRunner queryRunner = new QueryRunner(DruidUtil.getDataSource());
    @Override
    public List<Role> selectRoleByUserId(Long userId) {
        List<Role> roles = new ArrayList<>();
        String sql = "SELECT ro.id,ro.role FROM t_user u INNER JOIN r_user_role r ON u.id=r.user_id " +
                "INNER JOIN t_role ro ON r.role_id=ro.id where u.id=? ORDER BY ro.id;";
        BeanListHandler<Role> handler = BeanProcessor.getBeanListHandler(Role.class);
        try {
            roles = queryRunner.query(sql,handler,userId);
        }catch (Exception e){
            e.printStackTrace();
        }
        return roles;
    }
}
